package com.boshao.classschedule.utils;

/**
 * Data工具类
 */
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.util.DisplayMetrics;

import com.boshao.classschedule.model.DateInfo;
public class DataUtils {
	//检查单月日历是否可点击
	public static void checkLastSeven(List<DateInfo> list) {
		if (list.size() < 42)
			return;
		for (int i = 35; i < list.size(); i++) {
			if (list.get(i).getDate() != -1)
				return;
		}
		int j = 41;
		while (j >= 35) {
			list.remove(list.size() - 1);
			j--;
		}
	}
	/**
	 * 获取当月日历的第一天的位置
	 * @param list
	 * @return
	 */
	public static int getFirstIndexOf(List<DateInfo> list) {
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getDate() != -1)
				return i;
		}
		return -1;
	}
	
	/**
	 * 获取当月的最后一天的位置
	 * @param list
	 * @return
	 */
	public static int getLastIndexOf(List<DateInfo> list) {
		for (int i = list.size() - 1; i >= 0; i--) {
			if (list.get(i).getDate() != -1)
				return i;
		}
		return -1;
	}
	
	/**
	 * 返回某一天处理当月日历的位置
	 * @param list
	 * @param day
	 * @return
	 */
	public static int getDayFlag(List<DateInfo> list, int day) {
		int i;
		for (i = 0; i < list.size(); i++) {
			if (list.get(i).getDate() == day && list.get(i).isThisMonth()) {
				return i;
			}
		}
		for (i = list.size() - 1; i >= 0; i--) {
			if (list.get(i).isThisMonth()) {
				break;
			}
		}
		return i;
	}
	
	/**
	 * 根据日期获取viewpager的position位置
	 * @param date
	 * @param currentDate
	 * @return
	 */
	public static int getPositionFromCurrent(String date,String currentDate){//2015-05-01
		int position = -1;
		Calendar curCalendar = TimeUtils.getCalendarOfDate(currentDate);
		Calendar calendar = TimeUtils.getCalendarOfDate(date);
		int curYear = curCalendar.get(Calendar.YEAR);
		int year = calendar.get(Calendar.YEAR);
		int curMonth = curCalendar.get(Calendar.MONTH);
		int month = calendar.get(Calendar.MONTH);
		if(calendar.compareTo(curCalendar) == 0){
			position = 500;
		}else if(calendar.compareTo(curCalendar) == 1){
			position = countPosition(curYear, year, curMonth, month,true);
		}else{
			position = countPosition(year,curYear, month, curMonth,false);
		}
		return position;
	}

	/**
	 * 计算每个月日历的位置
	 * @param curYear
	 * @param year
	 * @param curMonth
	 * @param month
	 * @param after
	 * @return
	 */
	private static int countPosition(int curYear, int year, int curMonth,
			int month,boolean after) {
		int position;
		int flagMonth = 0;
		int flagYear = 0;
		boolean flag;
		if(month - curMonth >= 0){
			flagMonth = month - curMonth;
			flag = false;
		}else{
			flagMonth = month + 12 - curMonth;
			flag = true;
		}
		if(flag){
			flagYear = (year - 1 - curYear) * 12;
		}else{
			flagYear = (year - curYear) * 12;
		}
		if(after){
			position = 500 + flagMonth + flagYear;
		}else {
			position = 500 - flagMonth - flagYear;
		}
		return position;
	}
	
	/**
	 * 获取日历中最小日期
	 * @param date
	 * @return
	 */
	public static Calendar getMinCalendar(String date){
		Calendar calendar = TimeUtils.getCalendarOfDate(date);
		calendar.set(calendar.get(Calendar.YEAR)-40,calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
		return calendar;
	}
	
	/*
	 *获取日历中最大日期 
	 */
	public static Calendar getMaxCalendar(String date){
		Calendar calendar = TimeUtils.getCalendarOfDate(date);
		calendar.set(calendar.get(Calendar.YEAR)+40,calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
		return calendar;
	}
	
	public static int getScreenWidth(Activity activity) {
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		int width = mDisplayMetrics.widthPixels;
		return width;
	}
	
	public static int getScreenHeight(Activity activity) {
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		int height = mDisplayMetrics.heightPixels;
		return height;
	}
	
}
